<%@ page import="ro.gateway.aida.utils.HttpUtils,
                 java.io.*,
                 java.util.Locale,
                 ro.xblue.translator.Translator,
                 java.util.Hashtable,
                 ro.gateway.aida.servlet.Constants,
                 ro.xblue.translator.TranslatorConstants,
                 ro.xblue.translator.LoadTranslatorServlet"%>
<%
    File dir = new File(application.getRealPath("WEB-INF/translator"));
    if ((dir == null) || (!dir.isDirectory())) {
        response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED);
        return;
    }
    String ctx = request.getContextPath();
    File[] files = dir.listFiles();
    if (files == null) files = new File[0];

    String cm = HttpUtils.getValidTrimedString(request, "cm", null);
    if (cm == null) {
        cm = HttpUtils.getValidTrimedString(request, "cm1", null);
    }
    String cmodule = null; String clang = null;
    File cfile = null;
    if (cm != null) {
        int _index = cm.indexOf("~");
        if (_index > 0) {
            cmodule = cm.substring(0, _index);
            clang = cm.substring(_index + 1);
            cfile = new File(dir, cmodule + File.separatorChar + clang + ".properties");
            if (!cfile.exists()) {
                cfile = null;
                cmodule = null; clang = null;
            }
        }
    }

    String action = HttpUtils.getValidTrimedString(request, "action", null);
    String message = null;
    if ("save".equals(action) && (cfile != null)) {
        try {
            PrintWriter writer = new PrintWriter(
                    new FileWriter(cfile)
            );
            String content = request.getParameter("content");
            if (content != null) {
                writer.print(content);
                message = "Saved successfuly";
            }
            writer.close();

            Translator translator = (Translator) application.
                    getAttribute(TranslatorConstants.BLUE_TRANSLATOR);
            translator.reloadModule(cmodule);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if ("translator".equals(cmodule) && ("en".equals(clang))) {
            LoadTranslatorServlet.loadTranslator(application);
//            System.out.println( "translator reloaded" );
        }
    } else if ("delete".equals(action) && (cfile != null)) {
        cfile.delete();
        response.sendRedirect(ctx + "/translator/modules.jsp");
    } else if ("create".equals(action)) {
        String module = HttpUtils.getValidTrimedString(request, "module", null);
        String lang = HttpUtils.getValidTrimedString(request, "lang", null);
        if ((module == null) || (lang == null)) {
            return;
        }
        lang = lang.toLowerCase(); module = module.toLowerCase();
        Locale test = new Locale(lang);
        if (lang.equals(test.getDisplayLanguage())) {
            return;
        }
        File mdir = new File(dir, module);
        if (!mdir.exists()) {
            mdir.mkdirs();
        }
        File mfile = new File(mdir, lang + ".properties");
        if (!mfile.exists()) {
            mfile.createNewFile();
        }
    }
%>
<%@taglib uri="/WEB-INF/translator.tld" prefix="trans"  %>
<trans:initPage module="translator" />
<html>

<head>
<script language="javascript">
    function submit_theform(mode) {
        if (mode == 1) {
            document.theform.method='POST';
            if (document.theform.content) {
                document.theform.content.disabled = 1;
            }
            if (document.theform.cm1) {
                document.theform.cm1.disabled = 1;
            }
            document.theform.submit();
            disable_controls();
        } else {
            document.theform.method='POST';
            if (document.theform.content) {
                document.theform.content.disabled=1;
            }
            document.theform.cm.disabled=1;
            document.theform.submit();
            disable_controls();
        }
    };
    function save() {
        document.theform.action.value = 'save';
        document.theform.method = 'POST';
        document.theform.submit();
        disable_controls();
    };
    function dlete() {
        if (confirm("Confirmati stergerea")) {
            document.theform.action.value = 'delete';
            document.theform.submit();
            disable_controls();
        }
    };
    function create() {
        if (confirm("Confirmati crearea")) {
            otherform.action.value = "create";
            otherform.submit();
            disable_controls();
        }
    };
    function disable_controls() {
        if (document.theform.bsave) {
            document.theform.bsave.disabled = 1;
            document.theform.bsave1.disabled = 1;
            document.theform.bdelete.disabled = 1;
        }
        otherform.bcreate.disabled = 1;
        otherform.lang.disabled = 1;
        otherform.module.disabled = 1;
        document.theform.cm.disabled = 1;
    }
</script>
<title><trans:showPageMessage label="page.title" /><%=(cmodule != null) ? cmodule : ""%>, version: <%=(clang != null) ? clang : ""%></title>
<link rel="stylesheet" href="../alf.css" type="text/css"><LINK rel="stylesheet" type="text/css" href="<html:rewrite page='/alf.css'/>">
</head>

<body>
<jsp:include page="../header.jsp" />
<table align="center" width="75%" border="0" cellpadding="5" cellspacing="1" background=<%=ctx%>/images/pixelbgtable.gif class="text_search">
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" align="right"> <trans:langChanger showLabel="true" /> </td>
  </tr>
  <tr bgcolor="#FFFFFF"> 
    <td colspan="2" class="text_bold"> <a href="<%=ctx%>/admin.jsp"><trans:showMessage module="admin" label="page.pagename" /></a> 
      :: <trans:showMessage module="admin" label="page.modlng" /> </td>
  </tr>
  <form method="POST" action="<%=ctx%>/translator/modules.jsp" name="theform">
    <input type="hidden" name="action" value="chgmod" />
    <tr bgcolor="#FFFFFF"> 
      <td colspan="2" align="right" valign="top"> 
        <table width="100%" cellpadding="5" border="0">
          <tr> 
            <td width="50%" valign="top"> 
              <% if (cmodule != null) { %>
              <trans:showPageMessage label="page.wmod" />: <b><u><%=cmodule%></u></b><br>
              <trans:showPageMessage label="page.lmod" />: 
              <select name="cm1" onchange="javascript:submit_theform(2);">
                <%
                File mod_dir = new File(dir, cmodule);
                File[] versions = mod_dir.listFiles();
            %>
                <% for (int i = 0; i < versions.length; i++) { %>
                <%
                String fname = versions[i].getName();
                if (!fname.endsWith(".properties")) continue;
                String lng = fname.substring(0, fname.indexOf('.'));
                Locale l = new Locale(lng);
                %>
                <option <%=(clang.equals(lng)) ? "selected=\"true\" " : ""%>value="<%=cmodule%>~<%=lng%>"><%=l.getDisplayLanguage()%></option>
                <% } %>
              </select>
              <% } %>
            </td>
            <td width="50%" align="right" valign="top"> 
              <% if (files.length > 0) { %>
              <trans:showPageMessage label="page.quickgo" />: 
              <select name="cm" onchange="javascript:submit_theform(1);">
                <option disabled=1><trans:showPageMessage label="page.selmodule" /></option>
                <% for (int i = 0; i < files.length; i++) { %>
                <% if (files[i].isDirectory()) { %>
                <%
                    String module_name = files[i].getName();
                    File[] lfiles = files[i].listFiles();
                    if (lfiles == null) lfiles = new File[0];
                    %>
                <% for (int j = 0; j < lfiles.length; j++) { %>
                <%
                        String lang_file = lfiles[j].getName();
                        if (!lang_file.endsWith(".properties")) continue;
                        String lng = lang_file.substring(0, lang_file.lastIndexOf('.')).toLowerCase();
                        %>
                <option <%=(module_name.equalsIgnoreCase(cmodule) && lng.equalsIgnoreCase(clang)) ? "selected=\"true\" " : ""%>value="<%=module_name%>~<%=lng%>"><%=module_name%>
                - <%=new Locale(lng).getDisplayLanguage()%></option>
                <% } %>
                <% } %>
                <% } %>
              </select>
              <% } else { %>
              <trans:showPageMessage label="page.nomodule" /> 
              <% } %>
              <br>
              <a href="#emod"><trans:showPageMessage label="page.newmod" /></a> 
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <% if (cfile != null) { %>
    <% if (message != null) { %>
    <tr bgcolor="#FFFFFF"> 
      <td colspan="2" class="text_searchBold"><%=message%></td>
    </tr>
    <% } %>
    <tr bgcolor="#FFFFFF"> 
      <td valign="top" align="right" class="text_searchBold"> <trans:showPageMessage label="page.messages" />:<br>
        <br>
        <br>
        <input type="button" class="butonsave" name="bsave1" value="<trans:showPageMessage label="form.bsave" hide_link="true" />" onclick="javascript:save();" /> </td>
      <td valign="top"> 
        <textarea name="content" cols="65" rows="25"><%
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(new FileInputStream(cfile))
                );
                String line = reader.readLine();
                String necstLine = reader.readLine();
                while (true) {
                    if (line == null) break;
                    if (necstLine == null) {
                        out.print(line);
                    } else {
                        out.println(line);
                    }
                    line = necstLine;
                    necstLine = reader.readLine();
                }
                reader.close();
            } catch (Exception ex) {
                out.println(ex.getMessage());
            }
            %></textarea>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td colspan="2" align="center"> 
        <input type="button" name="bsave" class="butonsave" value="<trans:showPageMessage label="form.bsave" hide_link="true" />" onclick="javascript:save();" />
        <input type="button" name="bdelete" class="butonsave" value="<trans:showPageMessage label="form.delmod" hide_link="true" /><%=cmodule%> : <%=new Locale(clang).getDisplayLanguage()%>" onclick="javascript:dlete();" /> </td>
    </tr>
    <% } %>
  </form>
</table>




    <br>
    <br>
    <br>
    <a name="emod" />
    
<table align="center" width="500" border="0" cellpadding="5" cellspacing="1" background=<%=ctx%>/images/pixelbgtable.gif class="text_search">
  <form method="POST" action="<%=ctx%>/translator/modules.jsp" name="otherform">
    <input type="hidden" name="action" value="" />
    <tr bgcolor="#FFFFFF"> 
      <td colspan="2" class="text_searchBold"><trans:showPageMessage label="page.newmod" />:</td>
    <tr bgcolor="#FFFFFF"> 
      <td valign="top" align="right" class="text_searchBold"><trans:showPageMessage label="page.modname" />:</td>
      <td valign="top"> 
        <input type="text" name="module" value="<%=cmodule != null ? cmodule : ""%>" />
      </td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td valign="top" align="right" class="text_searchBold"><trans:showPageMessage label="page.lang" />:</td>
      <td valign="top"> 
        <select name="lang">
          <option><trans:showPageMessage label="form.sellang" /></option>
          <%
                String[] locales = Locale.getISOLanguages();
            %>
          <% for (int i = 0; i < locales.length; i++) {
                Locale locale = new Locale(locales[i]);
            %>
          <option value="<%=locales[i]%>"><%=locale.getDisplayLanguage()%></option>
          <% } %>
        </select>
      </td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td colspan="2" align="center"> 
        <input type="button" name="bcreate" class="butonsave" value="<trans:showPageMessage label="form.bcreate" hide_link="true" />" onclick="javascript:create();" /> </td>
    </tr>
  </form>
</table>
    <br><br>
	<jsp:include page="../footer.jsp" />
</body>

</html>
